﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Common;

namespace DataAccessLayer
{
    public class Master_Items_DB
    {
        Database objDatabase = null;
        DbCommand ObjCommand = null;
        MethodResult ObjMethodResult = null;

        public const string GETMASTERITEMS = "dbo.GetMasterItems";
        public const string ADDMASTERITEM = "AddMasterItem";
        public const string UPDATEMASTERITEM = "UpdateMasterItem";
        public const string DELETEMASTERITEM = "DeleteMasterItem";
        public const string GETMASTERITEMSFORRATEMASTER ="GetMasterItemsForRateMaster";


        public Master_Items_DB()
        {
            // Used to connect to more than one database
            DatabaseProviderFactory Objfactory = new DatabaseProviderFactory();
            objDatabase = Objfactory.CreateDefault();
            ObjMethodResult = new MethodResult();

        }

        public List<Master_Items> GetMasterItems(string master_item_type,bool status)
        {
            string query = "exec getmasteritems '" + master_item_type + "'," + status;
            ObjCommand = objDatabase.GetStoredProcCommand(GETMASTERITEMS);
                      
            var listusers = objDatabase.ExecuteSqlStringAccessor<Master_Items>(query);
            return listusers.ToList<Master_Items>();

        }


        public DataTable GetMasterItemsInDataTable(string master_item_type)
        {
            string query = "select item_value from Master_Items where Master_Item ='" + master_item_type + "' and Status = " + 1;
            return objDatabase.ExecuteDataSet(CommandType.Text, query).Tables[0];

        }

        public List<Master_Items> GetAllMastersForRateMaster()
        {
            var listusers = objDatabase.ExecuteSqlStringAccessor<Master_Items>(GETMASTERITEMSFORRATEMASTER);
            return listusers.ToList<Master_Items>();
        }

        public DataTable GetAllMastersForRateMasterDataTable()
        {
            DataTable listusers = objDatabase.ExecuteDataSet(GETMASTERITEMSFORRATEMASTER).Tables[0];
            return listusers;
        }

        public MethodResult AddMasterItem(string item_Type, string item_value,string desc, string user_id)
        {
           
            ObjCommand = objDatabase.GetStoredProcCommand(ADDMASTERITEM);
            objDatabase.AddInParameter(ObjCommand, "Master_Item", DbType.String, item_Type);
            objDatabase.AddInParameter(ObjCommand, "Item_Value", DbType.String, item_value);
            objDatabase.AddInParameter(ObjCommand, "Desc", DbType.String, desc);
            objDatabase.AddInParameter(ObjCommand, "User_Id", DbType.String, user_id);
            objDatabase.AddOutParameter(ObjCommand, "Return_Message", DbType.String, 100);
          
             short result = (short)objDatabase.ExecuteNonQuery(ObjCommand);

           string result_Message = objDatabase.GetParameterValue(ObjCommand, "Return_Message").ToString();
            if (result < 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Warning, result_Message, null, result);
            else if (result > 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Success, result_Message, null, result);
            else
                return ObjMethodResult.CurrentMessageResult(MessageType.Information, result_Message, null, result);

        }

        public MethodResult UpdateMasterItem(string item_Type, string item_value, string desc,bool status,string user_id)
        {

            ObjCommand = objDatabase.GetStoredProcCommand(UPDATEMASTERITEM);
            objDatabase.AddInParameter(ObjCommand, "Master_Item", DbType.String, item_Type);
            objDatabase.AddInParameter(ObjCommand, "Item_Value", DbType.String, item_value);
            objDatabase.AddInParameter(ObjCommand, "Desc", DbType.String, desc);
            objDatabase.AddInParameter(ObjCommand, "Status", DbType.String, status);
            objDatabase.AddInParameter(ObjCommand, "User_Id", DbType.String, user_id);
            objDatabase.AddOutParameter(ObjCommand, "Return_Message", DbType.String, 100);

            short result = (short)objDatabase.ExecuteNonQuery(ObjCommand);

            string result_Message = objDatabase.GetParameterValue(ObjCommand, "Return_Message").ToString();
            if (result < 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Warning, result_Message, null, result);
            else if (result > 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Success, result_Message, null, result);
            else
                return ObjMethodResult.CurrentMessageResult(MessageType.Information, result_Message, null, result);

        }

        public MethodResult DeleteMasterItem(string item_Type, string item_value,string user_id)
        {

            ObjCommand = objDatabase.GetStoredProcCommand(DELETEMASTERITEM);
            objDatabase.AddInParameter(ObjCommand, "Master_Item", DbType.String, item_Type);
            objDatabase.AddInParameter(ObjCommand, "Item_Value", DbType.String, item_value);
            objDatabase.AddInParameter(ObjCommand, "User_Id", DbType.String, user_id);
            objDatabase.AddOutParameter(ObjCommand, "Return_Message", DbType.String, 100);

            short result = (short)objDatabase.ExecuteNonQuery(ObjCommand);

            string result_Message = objDatabase.GetParameterValue(ObjCommand, "Return_Message").ToString();
            if (result < 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Warning, result_Message, null, result);
            else if (result > 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Success, result_Message, null, result);
            else
                return ObjMethodResult.CurrentMessageResult(MessageType.Information, result_Message, null, result);

        }

    }
}
